package serviceImpl;

import dao.MessageDAO;
import entity.User;
import serviceInterface.messageInterface;

import java.util.Scanner;

public class manageMessage implements messageInterface {

    /*
        sender向receiver发送消息,返回是否发送成功
        如果发送者的小组已经存在消息,则用户进行选择是否进行覆盖
     */
    public boolean sendMessage(User sender,User receiver) {
        //如果已经存在该小组的消息
        if (MessageDAO.groupMessageExist(sender)){
            System.out.println("该小组消息已经存在，是否对该消息进行覆盖?(Yes/No):");
            Scanner in=new Scanner(System.in);
            while (true) {
                String s = in.next();
                if ("yes".equalsIgnoreCase(s)) {
                    MessageDAO.deleteMessage(MessageDAO.getGroup_id(sender));
                    break;
                } else if ("no".equalsIgnoreCase(s)) {
                    return false;
                } else {
                    System.out.println("输入不合法,请重新输入(Yes/No):");
                }
            }
        }
        boolean bool = MessageDAO.sendMessage(sender, receiver);
        return bool;
    }

    //返回user所收到的消息
    public String getMessage(User user){
        String message = MessageDAO.MessageReceive(user);
        return message;
    }

    //删除user收到的所有消息,返回是否删除成功
    public boolean deleteMessage(User user){
        boolean bool = MessageDAO.deleteMessage(user);
        return bool;
    }

}
